$(document).ready(function() {

	petIndex.index_init();
})
var petIndex = {
	//接口地址
	url_api: 'http://www.ibabypet.com.cn/public/index.php/api/index/pchome', //首页轮播图，tab 商标
	url_api2: 'http://www.ibabypet.com.cn/public/index.php/api/goods/pro_list/', //商品列表
	//首页商品数据渲染
	//banner图获取数据渲染页面
	index_init: function() {
		var that = this;
		$.ajax({
			url: this.url_api,
			type: 'get',
			async: true,
			success: function(res) {
				res = JSON.parse(res);
				if(res.code == 200) {
					var data = res.data;
					//轮播
					that.lunbo(data);
					//商品tab
					that.goods_tabs_select(data);
					//左边商标
					that.brands(data);
					//左边广告渲染】
					that.advertisement(data);
					//跳页
					that.click_goods();

				} else {
					
				}
			},
			error: function(res) {
				
			}
		});
	},

	//轮播图
	lunbo: function(data) {
		//渲染轮播图b_data是个arr
		var b_data = data.banner;
		$.each(b_data, function(i, v) {
			var b_el;
			b_el = '<div class="section"><a href="' + v.url + '"><img src="/public/' + v.banner + '" alt=""></a></div>';
			$('#container .sections').append(b_el);
		});

		$("#container").PageSwitch({
			direction: 'horizontal',
			easing: 'ease-in',
			duration: 3000,
			autoPlay: true,
			loop: 'false'
		});
	},
	//商品tap按钮选择
	goods_tabs_select: function(data) {
		var that = this;
		//商品渲染
		var g_tab_data = data.attributes;
		$.each(g_tab_data, function(i, v) {
			var li_el;
			li_el = '<li data-id=' + v.id + '>' + v.name + '</li>';
			$('.goods .goods_title_rg ul').append(li_el);
			//初始页面 获取商品列表 根据第一个tabid获取商品列表
			if(i == 0) {
				var attributes_id = v.id;
				that.goods_list1(attributes_id);
				that.goods_list2(attributes_id);
				that.goods_list3(attributes_id);
			};
		});
		$('.dog_goods li:eq(0)').addClass('active');
		$('.cat_goods li:eq(0)').addClass('active');
		$('.other_pet_goods li:eq(0)').addClass('active');
		//点击tab标签 获取数据渲染页面
		$(document).on('click', '.goods_title_rg li', function() {
			$(this).addClass('active').siblings('li').removeClass('active');
			//商品列表数据重新渲染 根据id
			//判断哪个区里
			var data_id = $(this).data('id');
			var category = $(this).parents('.goods').data('catorgery');
			switch(category) {
				case 'dog':
					//清空商品列表重新渲染
					$('.dog_goods .goods_content_rg ul').empty();
					that.goods_list1(data_id);
					break;
				case 'cat':
					$('.cat_goods .goods_content_rg ul').empty();
					that.goods_list2(data_id);
					break;
				case 'pet':
					$('.other_pet_goods .goods_content_rg ul').empty();
					that.goods_list3(data_id);
					break;
				default:
					break;
			};
		});
	},

	//获取商品列表 
	goods_list1: function(goods_id) {
		$.ajax({
			type: "POST",
			url: this.url_api2,
			async: true,
			data: {
				property_id: goods_id
			},
			//			dataType: 'json',
			success: function(res) {
				res = JSON.parse(res);
				if(res.code == 200) {
					var data_arr = res.data.goods; //数据列表 
					$.each(data_arr, function(i, v) {
						if(i > 7) {
							return false;
						};
						var li_el;
						li_el = '<li data-goodsId="'+v.id+'"><div class="goods_pic"><img src="/public' + v.logo + '" /></div><p>' + v.name + '</p>' +
							'<div class="goods_price_wrap"><div class="original_price lf"><span>&#65509;</span>' +
							'<span>' + v.sell_price + '</span></div><div class="member_price lf">' +
							'' +
							'</div><div class="goods_car rg"></div></div></li>';
						$('.dog_goods .goods_content_rg ul').append(li_el);

					});

				} else {
				};
			},
			error: function(res) {
			}
		});
	},
	goods_list2: function(goods_id) {
		$.ajax({
			type: "POST",
			url: this.url_api2,
			async: true,
			data: {
				property_id: goods_id
			},
			//			dataType: 'json',
			success: function(res) {
				res = JSON.parse(res);
				if(res.code == 200) {
					var data_arr = res.data.goods; //数据列表 
					$.each(data_arr, function(i, v) {
						if(i > 7) {
							return false;
						};
						var li_el;
						li_el = '<li data-goodsId="'+v.id+'"><div class="goods_pic"><img src="/public' + v.logo + '" /></div><p>' + v.name + '</p>' +
							'<div class="goods_price_wrap"><div class="original_price lf"><span>&#65509;</span>' +
							'<span>' + v.sell_price + '</span></div><div class="member_price lf">' +
							'' +
							'</div><div class="goods_car rg"></div></div></li>';
						//						$('.dog_goods .goods_content_rg ul').append(li_el);
						$('.cat_goods .goods_content_rg ul').append(li_el);
						//						$('.other_pet_goods .goods_content_rg ul').append(li_el);

					});

				} else {
				};
			},
			error: function(res) {
			}
		});
	},
	goods_list3: function(goods_id) {
		$.ajax({
			type: "POST",
			url: this.url_api2,
			async: true,
			data: {
				property_id: goods_id
			},
			success: function(res) {
				res = JSON.parse(res);
				if(res.code == 200) {
					var data_arr = res.data.goods; //数据列表 
					$.each(data_arr, function(i, v) {
						if(i > 7) {
							return false;
						};
						var li_el;
						li_el = '<li data-goodsId="'+v.id+'"><div class="goods_pic"><img src="/public' + v.logo + '" /></div><p>' + v.name + '</p>' +
							'<div class="goods_price_wrap"><div class="original_price lf"><span>&#65509;</span>' +
							'<span>' + v.sell_price + '</span></div><div class="member_price lf">' +
							'' +
							'</div><div class="goods_car rg"></div></div></li>';
						$('.other_pet_goods .goods_content_rg ul').append(li_el);
					});

				} else {
				};
			},
			error: function(res) {
			}
		});
	},
	//左侧品牌
	brands: function(data) {
		var that = this;
		//商品渲染
		var data_arr = data.brands;
		$.each(data_arr, function(i, v) {
			var li_el;
			li_el = '<li data-id=' + v.id + '>' + v.name + '</li>';
			$('.goods_content_wrap .brand ul').append(li_el);
		});
		//点击商标获取数据渲染商品页面
		$('.brand li').unbind('click').on('click', function(){
			event.stopPropagation();
			var brand_id = $(this).data('id');
			//判断不同的狗狗 猫咪 小宠不同的区，分别获取数据渲染不同的页面
			var kinds = $(this).parents('.goods').data('catorgery');
			switch(kinds) {
				case 'dog':
					$('.dog_goods .goods_content_rg ul').empty();
					that.init_goods_list(brand_id, '.dog_goods .goods_content_rg ul');
					break;
				case 'cat':
					$('.cat_goods .goods_content_rg ul').empty();
					that.init_goods_list(brand_id, '.cat_goods .goods_content_rg ul');
					break;
				case 'pet':
					$('.other_pet_goods .goods_content_rg ul').empty();
					that.init_goods_list(brand_id, '.other_pet_goods .goods_content_rg ul');
					break;
				default:
					break;
			};
			return false;
		});
	},
	//根据品牌id渲染的页面
	init_goods_list: function(brand_id, list_el) {
		var that = this;
		$.ajax({
			type: "POST",
			url: that.url_api2,
			async: true,
			data: {
				brand_id: brand_id
			},
			success: function(res) {
				res = JSON.parse(res);
				console.log('点击brand获取到的数据', res)
				if(res.code == 200) {
					var data_arr = res.data.goods; //数据列表 
					$.each(data_arr, function(i, v) {
						if(i > 7) {
							return false;
						};
						var li_el;
						li_el = '<li data-goodsId="'+v.id+'"><div class="goods_pic"><img src="/public' + v.logo + '" /></div><p>' + v.name + '</p>' +
							'<div class="goods_price_wrap"><div class="original_price lf"><span>&#65509;</span>' +
							'<span>' + v.sell_price + '</span></div><div class="member_price lf">' +
							'' +
							'</div><div class="goods_car rg"></div></div></li>';
						$(list_el).append(li_el);
					});

				} else {
				};
			},
			error: function(res) {
			}
		});
	},
	//左边广告的渲染
	advertisement: function(data) {
		var data_arr = data.advertising;
		var a, b, c;
		var a_url, b_url, c_url;
		$.each(data_arr, function(i, v) {
			//取出前三个广告的图片
			if(i > 2) {
				return false;
			};
			if(i == 0) {
				a = v.image;
				a_url = v.url;
			} else if(i == 1) {
				b = v.image;
				b_url = v.url;
			} else if(i == 2) {
				c = v.image;
				c_url = v.url;
			};
		});
		$('.goods_content_lf').eq(0).css('background-image', 'url(/public' + a + ')');
		$('.goods_content_lf').eq(1).css('background-image', 'url(/public' + b + ')');
		$('.goods_content_lf').eq(2).css('background-image', 'url(/public' + c + ')');
		//阻止事件冒泡 （点击子元素不会冒泡到父元素，执行父元素的事件）
		$('.brand').on('click', function(e){
			console.log('e', e)
			e.stopPropagation();
		});
		$('.goods_content_lf').eq(0).unbind('click').on('click', function(event) {
			event.stopPropagation();
			console.log('a_url', a_url)
			location.href = a_url;
			return false;
		});
		$('.goods_content_lf').eq(2).unbind('click').on('click', function() {
			event.stopPropagation();
			location.href = b_url;
			return false;
			
		});
		$('.goods_content_lf').eq(3).unbind('click').on('click', function() {
			event.stopPropagation();
			location.href = c_url;
			return false;
			
		});
	},
	
	//点击单个商品跳到详情页
	click_goods: function(){
		$(document).on('click', '.goods_content_rg li', function(){
			var goodsId = $(this).data('goodsid');
			window.location.href = '/pcpet/product_detail.html?goodsId='+goodsId;
		})
	}

}